package org.pr.ddddesigner.infrastructure.support;

import ch.qos.logback.core.joran.node.ComponentNode;
import com.lion.commons.tools.scene.Scene;
import com.xw.soonmanager.component.dto.BuildComponentRequestDto;
import com.xw.soonmanager.filemanager.ModelType;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import reactor.core.publisher.Mono;

import javax.xml.transform.Result;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

/**
 * @author chenbin
 */
public interface ComponentBuilder {
    boolean support(ModelType type);

    Mono<Result> build(BuildComponentRequestDto metadata);

    /*static ModelComponent createComponent(BuildComponentRequestDto metadata) {
        ModelComponent component = ModelComponent.createNew();
        component.setDirectoryId(metadata.getDirectoryId());
        component.setProjectId(metadata.getProjectId());
        component.setName(metadata.getName());
        component.setCreateUser(metadata.getUserId());
        return component;
    }

    static ComponentEdition createEdition(BuildComponentRequestDto metadata) {
        ComponentEdition edition = ComponentEdition.createNew();
        edition.setFileId(metadata.getFileId());
        edition.setProjectId(metadata.getProjectId());
        edition.setVersion(1L);
        edition.setCreateUser(metadata.getUserId());
        return edition;
    }

    static ComponentNode createNode(BuildComponentRequestDto metadata) {
        ComponentNode node = ComponentNode.createNew();
        node.setFileId(metadata.getFileId());
        node.setProjectId(metadata.getProjectId());
        node.setCreateUser(metadata.getUserId());
        return node;
    }

    static List<ComponentNode> extractNodes(Scene scene, Supplier<ComponentNode> nodeSupplier) {
        Map<String, ComponentNode> nodeMap = new LinkedHashMap<>();
        scene.stream().forEach(item -> {
            ComponentNode node = nodeSupplier.get();
            node.setLocation(item.getLocation());
            node.setName(item.getName());
            node.setRenderType(item.getRenderType());
            node.setMatrix(item.getMatrix());

            ComponentNode parent = nodeMap.get(item.getPid());
            if (parent != null) {
                node.setPid(parent.getId());
            }

            nodeMap.put(item.getId(), node);
        });
        return new ArrayList<>(nodeMap.values());
    }

    @Getter
    @Setter
    @NoArgsConstructor
    @AllArgsConstructor
    class Result {
        private ModelComponent component;
        private ComponentEdition edition;
        private List<ComponentNode> nodes;
    }*/
}
